package com.usian.wemedia.controller;

import com.usian.apis.wemedia.WmMaterialControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.service.WmMaterialService;
import com.usian.wemedia.utils.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/28 15:14
 */
@RestController
@RequestMapping("/wmMaterial")
public class WmMaterialController implements WmMaterialControllerApi {

    @Autowired
    AliyunOSSClientUtil aliyunOSSClientUtil;

    @Autowired
    WmMaterialService wmMaterialService;
    @Override
    @PostMapping("/uploadFile")
    public ResponseResult uploadFile(MultipartFile multipartFile) {
        try {
            String img2Oss = aliyunOSSClientUtil.uploadImg2Oss(multipartFile);
            String imgUrl = aliyunOSSClientUtil.getImgUrl(img2Oss);
            WmUser user = WmThreadLocalUtils.getUser();
            Integer userId = user.getId();
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUserId(userId);
            wmMaterial.setUrl(imgUrl);
            wmMaterial.setIsCollection((short)1);
            wmMaterial.setType((short)1);

            wmMaterialService.saveWmMaterial(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}
